package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import model.ComplaintModel;
import view.CompareReportGUI;
import client.CliMessage;

/**
 * The Class CompareReportController that extends AbstractController implements IValidateInput.
 * @author yudku
 */
public class CompareReportController extends AbstractController implements IValidateInput
{		
	/** The Compare view. */
	private CompareReportGUI CompareView;
	
	/** The Complaint model. */
	private ComplaintModel ComplaintModel;
	
	/** The count. */
	static int  count;
	
	/**
	 * Instantiates a new compare report controller.
	 *
	 * @param CompareView the compare view
	 * @param ComplaintModel the complaint model
	 */
	public CompareReportController(CompareReportGUI CompareView, ComplaintModel ComplaintModel) 
	{
		super(CompareView,ComplaintModel);
		this.CompareView = CompareView;
		this.ComplaintModel = ComplaintModel;
	}

	@Override
	public boolean validateInput() 
	{	
		return true;		
	}

	class ReplyListener implements ActionListener
	{
		
		/** The Comp model. */
		private ComplaintModel CompModel;
		
		/**
		 * Instantiates a new reply listener.
		 *
		 * @param CompModel the comp model
		 */
		public ReplyListener(ComplaintModel CompModel)
		{
			this.CompModel=CompModel;
		}
		
		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			if(!validateInput())
				return;

		}
	}

	@Override
	public void handleDBRequestSuccess()
	{
		CompareView.displayWrnMessage("Reply Succesfully Enterd");
		
	}

	@Override
	public void handleDBSearchResult(CliMessage dbResult) 
	{	
		ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
		int rows=srs.size();
		int i;
		Map<String, String> dateList  = srs.get(0);	
		
		    Map<String, String> tReq  = srs.get(0);		    
			if ( dateList.get("CID") != null )
			{
				System.out.println ("COMPLAINT DATABASE size: " + tReq.size());
				String comp;
				comp = "Over all Complaints: " + tReq.get("COMPLAINTNUM") +  "\n\nUnhandeled Complaints: " + tReq.get("UNHANDLEDCOMPLAINTS") + "\n\nMonthly Handeled :" + tReq.get("MONTHLYHANDLED");
				if (count == 0)  
					CompareView.setReportArea1(comp);
				else
					CompareView.setReportArea2(comp);
					
				tReq.clear();
			}
			
			if ( dateList.get("AID") != null )
			{
				System.out.println ("ARCHIVE DATABASE size: " + tReq.size());
				String arch;
				arch = "Photos in Archive: " + tReq.get("ARCHIVESIZE") + "\nMonthley Download rate: " + tReq.get("NEWPHOTO") + "\ndetails:" + tReq.get("PHOTODET");
				if (count == 0)  
					CompareView.setReportArea1(arch);
				else
					CompareView.setReportArea2(arch);
				
				tReq.clear();
			}
			
			if ( dateList.get("PID") != null )
			{
				System.out.println ("PACKAGE DATABASE size: " + tReq.size());
				String pack;
				pack = tReq.get("DETAILS");
				if (count == 0)  
					CompareView.setReportArea1(pack);
				else
					CompareView.setReportArea2(pack);
					
				tReq.clear();
			}
		//display main window
		CompareView.setVisible(true);		
		count ++;
	}

	@Override
	public void handleNullSearchResult() 
	{
		CompareView.displayWrnMessage("Unhandle Complaint Not Found!!!");	
	}

}